#include "Game.h"

const int TetradNum = 3;
int bWidth, bHeight;

Game::Game(int x, int y, char* a_filename, char** BlockFiles)
:BlockX(x), BlockY(y)
{
	m.loadMapFromFile(a_filename);

	for(int i = 0; i < TetradNum; ++i)
	{
		BlockFiles[i];
	}
	b.loadBlockFiles(BlockFiles[RandomBlock(BlockFiles)]);
	temp = NULL;
}

int Game::RandomBlock(char** BlockFiles)
{
	srand((unsigned)time(NULL));

	int RandBlock = (rand() % TetradNum);

	return RandBlock;
}

void Game::GetInput()
{
	input = _getch();
}

void Game::gotoxy(int x, int y)
{
	COORD point = {x,y};
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), point);
}

void Game::Move()
{
	switch(input)
		{
		case 'a':
			{
				BlockX--;
				input = 0;
				break;
			}
		case'd':
			{
				BlockX++;
				input = 0;
				break;
			}
		case 'r':
			{
				RotateCW();
				break;
			}
		}
}

void Game::update(char** BlockFiles)
{
	if(isWithin(0,0,m.getWidth(),m.getHeight()))
	{
		BlockY++;

		Move();

		if(BlockY + b.getBHeight() == m.getHeight())
		{
			Collision(BlockFiles);
		}
		else
		{
			//if(b.m_Block[BlockY + f][BlockX + c] == '.' && m.m_Map[BlockY + f][BlockX + c] == '*')
			//		b.m_Block[f][c] = '*';
			for(int z=0; z < b.getBWidth(); ++z)
			{
				if(m.m_Map[BlockY + b.getBHeight()][BlockX + z] == '*')
				{
					Collision(BlockFiles);
				}
			}
		}

		int AstCnt = 0;
		//ERASING LINES WHEN FULL OF *
		for(int col = m.getHeight() - 1; col >= 1 ; col--)
		{
			//Count 'x'
			for(int row = 0; row < m.getWidth(); ++row)
			{
				if(m.m_Map[col][row] == '*')
				AstCnt = AstCnt + 1;
			}

			do
			{
				CheckFullFlag = false;
				if(AstCnt == m.getWidth())
				{
					for(int row2 = 0; row2 < m.getWidth(); ++row2)
					{
						//Dropping the row;
						m.m_Map[col][row2] = m.m_Map[col - 1][row2];

						//Count'x'
						for(int row3 = 0; row3 < m.getWidth(); ++row3)
						{
							if(m.m_Map[col][row3] == '*')
								AstCnt = AstCnt + 1;	
						}
						if(AstCnt == m.getWidth())
							CheckFullFlag = true;
					}
				}
			}while(CheckFullFlag);

			//changing all the top symbols to '.'
			if(col == 1)
			{
				for(int row3 = 0; row3 < m.getWidth(); ++row3)
				{
					m.m_Map[col - 1][row3] = '.';
				}
			}

			AstCnt = 0;
		}
	}
}
void Game::RotateCW()
{
	temp = new char *[b.getBWidth()];
	for(int i = 0; i < b.getBWidth(); ++i)
	{
		temp[i] = new char [b.getBHeight()];
	}

	for(int col = 0; col < b.getBWidth(); ++col)
	{
		for(int row = 0; row < b.getBHeight(); ++row)
		{
			temp[col][row] = b.m_Block[(b.getBHeight()-1) - row][col];
		}
	}

	for(int col = 0; col < b.getBWidth(); col++)
	{
		for(int row = 0; row < b.getBHeight(); row++)
		{
			b.m_Block[col][row] = temp[col][row];
		}
	}
	int switching = b.getBHeight();
	b.SetHeight(b.getBWidth());
	b.SetWidth(switching);
	input = 0;
}

bool Game::isWithin(int MinX, int MinY, int MaxX, int MaxY)
{
	return BlockX >= MinX && BlockX <= MaxX - b.getBWidth() + 1 && BlockY >= MinY - 1 && BlockY < MaxY - b.getBHeight() + 1;
}

void Game::DrawBlock(int BlockX, int BlockY)
{
	BlockX = b.getX();
	BlockY = b.getY();
	RECT destRect;
	destRect.left = BlockX;
	destRect.top = BlockY;
	destRect.bottom = destRect.top + b.getBHeight();
	destRect.right = destRect.left + b.getBWidth();

	// draw the sprite to the back buffer
	Dx.blitToSurface(spriteSurface, &spriteStruct.srcRect, &destRect);
}

void Game::Draw(int x, int y)
{
	gotoxy(x,y);
	for(int i = 0; i < m.getHeight(); ++i)
	{
		for(int s = 0; s < m.getWidth(); ++s)
		{
			putchar(m.m_Map[i][s]);
		}
		putchar('\n');
	}
	putchar('\n');
}

void Game::Collision(char** BlockFiles)
{
	for(int f = 0; f < b.getBHeight(); ++f)
	{
		for(int c = 0; c < b.getBWidth(); ++c)
		{
			gotoxy(BlockX + c, BlockY + f);
			m.m_Map[BlockY + f][BlockX + c] = b.m_Block[f][c];

		}
	}
	Zeroize(BlockFiles);
}
void Game::Zeroize(char** BlockFiles)
{
	BlockX = 6;
	BlockY = 0;
	gotoxy(BlockX, BlockY);
	input = 0;
	b.loadBlockFiles(BlockFiles[RandomBlock(BlockFiles)]);
}

bool Game::IsStillRunning()
{
	return input != 'q';
}

Game::~Game(){}